﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL;
using Common;
using Entity;
using System.Text;
using System.Data;

namespace Web
{
    public partial class EditMyInfo : System.Web.UI.Page
    {
        protected UserLinkExtendInfo ULEInfo = new UserLinkExtendInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            ULEInfo = UserValidate.GetInfo(base.Request);
            if (ULEInfo.Id == 0)
            {
                ULEInfo = UserValidate.TeacherGetInfo(base.Request);
            }
            if (this.ULEInfo.Id == 0)
            {
                base.Response.Redirect("Login.aspx");
                base.Response.End();
            }
            //if (PageValidate.QueryString("Action") == "Save")
            //{
            //    this.Save();
            //}
            
            if (!IsPostBack)
            {
                BindSchool();
                if (ULEInfo.SchoolId > 0)
                {
                    BindSchoolSetItem(ULEInfo.SchoolId);
                }
                else
                {
                    BindSchoolSetItem(0);
                }
            }
        }
        private void BindSchoolSetItem(int schoolId)
        {
            SchoolSetInfo info = SchoolSet.GetInfoBySchoolId(schoolId);
            string[] arr = new string[] { };
            if (info != null && !PageValidate.IsEmpty(info.ChartStr))
            {
                arr = info.ChartStr.Split(',');
            }
            this.chkSchoolSetList.Items.Clear();
            foreach (SchoolSetItem ssitem in Enum.GetValues(typeof(SchoolSetItem)))
            {
                ListItem item = new ListItem();
                item.Text = ssitem.ToString();
                item.Value = ((int)ssitem).ToString();
                if (arr.Contains(((int)ssitem).ToString()))
                {
                    item.Selected = true;
                }
                else
                {
                    item.Selected = false;
                }
                this.chkSchoolSetList.Items.Add(item);
            }
        }
        public string ChkSelected(object obj1, object obj2)
        {
            if (obj1.ToString() == obj2.ToString())
            {
                return " selected";
            }
            return "";
        }
        private void BindSchool()
        {
            IList<SchoolInfo> list = School.GetList();
            foreach (SchoolInfo info in list)
            {
                ListItem item = new ListItem();
                item.Text = info.Name;
                item.Value = info.Id.ToString();
                if (ULEInfo.SchoolId == info.Id)
                    item.Selected = true;
                this.schoolList.Items.Add(item);
            }
        }
        
        private void Save()
        {
            string password = Utility.MD5(PageValidate.Form("OldPassword"));
            if (!UserLinkExtend.LoginUser(this.ULEInfo.IdCard, password, ref this.ULEInfo))
            {
                //base.Response.Write(ReturnCommon.ReturnTextJsonErrMessage("当前密码不正确", new object[0]));
                Utility.ShowJsAlertAndHistoryBack("当前密码不正确", this);
                return;
                //base.Response.End();
            }
            string inputKey = "";
            if (!PageValidate.FormIsEmpty("NewPassword") && !PageValidate.FormIsEmpty("ReNewPassword"))
            {
                if (PageValidate.Form("NewPassword") == PageValidate.Form("ReNewPassword"))
                {
                    inputKey = Utility.MD5(PageValidate.Form("NewPassword"));
                }
                else
                {
                    //base.Response.Write(ReturnCommon.ReturnTextJsonErrMessage("两次密码输入不一致", new object[0]));
                    Utility.ShowJsAlertAndHistoryBack("两次密码输入不一致", this);
                    return;
                    //base.Response.End();
                }
            }
            string inputString = PageValidate.Form("Email");
            if (!PageValidate.IsEmpty(inputString))
            {
                if (this.ULEInfo.Email != inputString)
                {
                    if (Users.CheckUserExistByEmail(inputString))
                    {
                        //base.Response.Write(ReturnCommon.ReturnTextJsonErrMessage("Email地址已存在", new object[0]));
                        Utility.ShowJsAlertAndHistoryBack("地址已存在", this);
                        return;
                        //base.Response.End();
                    }
                }
            }
            string str4 = PageValidate.Form("Intro");
            if (!PageValidate.IsEmpty(inputString) || !PageValidate.FormIsEmpty(inputKey))
            {
                UserInfo userInfoById = Users.GetUserInfoById(this.ULEInfo.Id);
                if (!PageValidate.IsEmpty(inputString))
                {
                    userInfoById.Email = inputString;
                }
                if (!PageValidate.IsEmpty(inputKey))
                {
                    userInfoById.PassWord = inputKey;
                }
                Users.Edit(userInfoById);
            }
            byte num = 0;
            if (PageValidate.FormIsEmpty("Business"))
            {
                //base.Response.Write(ReturnCommon.ReturnTextJsonErrMessage("一般系统保护性错误", new object[0]));
                Utility.ShowJsAlertAndHistoryBack("一般系统保护性错误", this);
                return;
                //base.Response.End();
            }
            if (!PageValidate.IsNumber(PageValidate.Form("Business")))
            {
                //base.Response.Write(ReturnCommon.ReturnTextJsonErrMessage("一般系统保护性错误", new object[0]));
                Utility.ShowJsAlertAndHistoryBack("一般系统保护性错误", this);
                return;
                //base.Response.End();
            }
            num = byte.Parse(PageValidate.Form("Business"));
            byte num2 = 0;
            if (PageValidate.FormIsEmpty("Caste"))
            {
                //base.Response.Write(ReturnCommon.ReturnTextJsonErrMessage("一般系统保护性错误", new object[0]));
                Utility.ShowJsAlertAndHistoryBack("一般系统保护性错误", this);
                return;
                //base.Response.End();
            }
            if (!PageValidate.IsNumber(PageValidate.Form("Caste")))
            {
                //base.Response.Write(ReturnCommon.ReturnTextJsonErrMessage("一般系统保护性错误", new object[0]));
                Utility.ShowJsAlertAndHistoryBack("一般系统保护性错误", this);
                return;
                //base.Response.End();
            }
            num2 = byte.Parse(PageValidate.Form("Caste"));
            int inputId = 0;
            if (PageValidate.FormIsEmpty("schoolList"))
            {
                //base.Response.Write(ReturnCommon.ReturnTextJsonErrMessage("一般系统保护性错误", new object[0]));
                Utility.ShowJsAlertAndHistoryBack("一般系统保护性错误", this);
                return;
                //base.Response.End();
            }
            if (!PageValidate.IsNumber(PageValidate.Form("schoolList")))
            {
                //base.Response.Write(ReturnCommon.ReturnTextJsonErrMessage("一般系统保护性错误", new object[0]));
                Utility.ShowJsAlertAndHistoryBack("一般系统保护性错误", this);
                return;
                //base.Response.End();
            }
            inputId = int.Parse(PageValidate.Form("schoolList"));
            if (PageValidate.FormIsEmpty("Tel"))
            {
                //base.Response.Write(ReturnCommon.ReturnTextJsonErrMessage("请填写联系电话", new object[0]));
                Utility.ShowJsAlertAndHistoryBack("一般系统保护性错误", this);
                return;
                //base.Response.End();
            }
            string str5 = PageValidate.Form("Tel");
            string name = School.GetSingleInfo(inputId).Name;
            UserExtendInfo infoByIdCard = UserExtend.GetInfoByIdCard(this.ULEInfo.IdCard);
            infoByIdCard.Business = num;
            infoByIdCard.Caste = num2;
            infoByIdCard.Intro = str4;
            infoByIdCard.SchoolId = inputId;
            infoByIdCard.School = name;
            infoByIdCard.Tel = str5;
            UserExtend.Save(infoByIdCard);
            //base.Response.Write(ReturnCommon.ReturnTextJsonSucceedMessage("保存成功"));
            SchoolSetInfo ssinfo = SchoolSet.GetInfoBySchoolId(ULEInfo.SchoolId);
            if (ssinfo == null)
            {
                ssinfo = new SchoolSetInfo();
                ssinfo.SchoolId = ULEInfo.SchoolId;
                ssinfo.ChartStr = CheckBoxListHelper.SelectedValue(this.chkSchoolSetList);
            }
            else
            {
                ssinfo.ChartStr = CheckBoxListHelper.SelectedValue(this.chkSchoolSetList);
            }
            SchoolSet.Save(ssinfo);
            Utility.ShowJsAlertAndHistoryBack("保存成功", this);
            return;
            //base.Response.End();
        }

        protected void schoolList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindSchoolSetItem(int.Parse(this.schoolList.SelectedValue));
        }

        protected void SaveBut_Click(object sender, EventArgs e)
        {
            this.Save();
        }
    }
}
